package com.adc.douyu_assistant.client;

import com.adc.douyu_assistant.entity.DouyuMessage;
import com.adc.douyu_assistant.entity.DouyuMessageHeader;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class DouyuMessageEncoder extends MessageToByteEncoder<DouyuMessage> {
    private final static int DY_MESSAGE_TYPE_CLIENT = 689;

    @Override
    protected void encode(ChannelHandlerContext ctx, DouyuMessage message, ByteBuf out) throws Exception {
        DouyuMessageHeader header = message.getMessageHeader();

        out.writeIntLE(header.getLength_length() + 8);
        out.writeIntLE(header.getHeader_length() + 8);
        out.writeShortLE(header.getHeader_type());
        out.writeByte(header.getHeader_encry());
        out.writeByte(header.getHeader_remain());

        out.writeBytes(message.getMessageContent().getBytes());
    }
}
